<template>
  <div class="login_box flex-c-c">
    <div class="login_form">
      <!-- logo -->
      <div class="logo_box flex-c-c" style="flex-direction: column">
        <!-- img的srcset属性是根据不同尺寸加载不同资源 -->
        <img src="../static/image/logo.png" alt="logo" />
        <div style="margin: 10px 0">欢迎！</div>
        <div>智学商城后台管理系统</div>
      </div>
      <div class="form_box flex-c-c">
        <div style="width: 330px">
          <div>登录</div>
          <el-input
            class="m-t-20"
            v-model.trim="username"
            placeholder="用户名"
            size="large"
          >
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
          <el-input
            class="m-t-20"
            type="password"
            v-model.trim="password"
            placeholder="密码"
            size="large"
            show-password
          >
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
          <el-button
            class="m-t-20"
            @click="submit"
            style="width: 150px"
            type="primary"
            size="large"
            >登录</el-button
          >
          <div class="m-t-20">作者：雷晴</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { get } from "@/request/http";
import Cookie from "js-cookie";
import { useRoute, useRouter } from "vue-router";

import { ElMessage } from "element-plus";
// pinia
import { storeToRefs } from "pinia";
import useStore from "@/stores";
const { admin } = useStore();

const password = ref("1234567");
const username = ref("admin");
const route = useRoute(); //路由数据对象
const router = useRouter(); //路由对象
// 提交登录
const submit = (): void => {
  if (password.value && username.value) {
    get({
      password: password.value,
      username: username.value,
      verifyCode: "1", //验证码必须要写点东西
      _gp: "admin",
      _mt: "login",
    }).then((res: any) => {
      if (200 == res.errno) {
        // 通过数据中的时间戳，获取添加30分钟的时间
        let expiresTime = new Date(res.timestamp + 60 * 1000 * 30);
        Cookie.set("token", res.data, { expires: expiresTime });
        getAdminInfo();
        let { back } = route.query;
        if (back) {
          router.replace(back as string);
        } else {
          router.replace("/");
        }
        ElMessage.success("登录成功");
      }
    });
  } else {
    ElMessage.error("请把表单填写完整");
  }
};
// 获取管理员信息
const getAdminInfo = () => {
  get({ _gp: "admin", _mt: "info" }).then((res) => {
    let { errno, data } = res;
    if (200 == errno) {
      let { user } = storeToRefs(admin) as any;
      user.value = data;
    }
  });
};
</script>

<style scoped lang="less">
.login_box {
  background: url(@/static/image/login_bg.jpg) center/cover no-repeat;
  width: 100%;
  height: 100vh;

  .login_form {
    background: #fff;
    width: 60%;
    min-height: 60%;
    display: flex;

    .logo_box {
      background: #005de9;
      width: 40%;
      color: #fff;

      img {
        width: 80px;
        box-shadow: #fff 3px -3px 15px 1px;
        border-radius: 50%;
      }
    }

    .form_box {
      /*表单盒子*/
      width: 60%;
      text-align: center;
    }
  }
}
</style>
